﻿
// test_11.16.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int run_years(int i)
{
	if (((i % 4 == 0) && (i % 100 != 0)) || (i % 400 == 0))
	{
		/*printf("%d是闰年！", i);*/
		return 1;
	}
	else
	{
		/*printf("%d不是闰年！", i);*/
		return 0;
	}
}



void menu1()
{
	int y = 0;
	int m = 0;
	int d = 0;
	int ds = 0;
	int r = 0;
	int  arr1[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	int arr2[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
	int i = 0;
	printf("请输入日期（yyyy-mm-dd）: \n");
	scanf("%d-%d-%d", &y, &m, &d);
	r = run_years(y);
	if (r == 1)
	{
		if (d > arr2[m - 1])
		{
			printf("日期错误！\n");
		}
		else
		{
			for (m; m > 1; m--)
			{
				ds += arr2[i];
				i++;
			}
			ds += d;
			printf("这是%d年的第%d天.\n", y, ds);
		}
	}
	else
	{
		if (d > arr1[m - 1])
		{
			printf("日期错误！\n");
		}
		else
		{
			for (m; m > 1; m--)
			{
				ds += arr1[i];
				i++;
			}
			ds += d;
			printf("这是%d年的第%d天.\n", y, ds);
		}
	}

}


void menu2()
{
	int y = 0;
	int d = 0;
	int ms = 1;
	int ds = 0;
	int r = 0;
	int i = 0;
	int arr1[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };
	int arr2[12] = { 31,29,31,30,31,30,31,31,30,31,30,31 };
	printf("请输入年和天数（空格分隔）: \n");
	scanf("%d%d", &y, &d);
	ds = d;
	r = run_years(y);
	if (r == 1)
	{
		while (ds > arr2[i])
		{
			ds -= arr2[i];
			i++;
			ms++;
		}
	}
	else
	{
		while (ds > arr1[i])
		{
			ds -= arr1[i];
			i++;
			ms++;
		}
	}
	printf("%d年的第%d天是：%d月%d日.\n", y, d, ms, ds);
}

void  run_menu()
{
	char a = '0';
	do
	{
		printf("===============[日期转换]============\n");
		printf("[1]. 给定日期，求这一年第几天?\n");
		printf("[2]. 给定年和天数，求日期？\n");
		printf("[x]. 退出\n");
		printf("=====================================\n");
		scanf("%s", &a);
		switch (a)
		{
		case '1':
			menu1();
			break;
		case '2':
			menu2();
			break;
		case 'x':
			printf("再见!\n");
			break;
		default:
			printf("输入错误，请重新输入\n");
			break;
		}

	} while (a != 'x');
}


int main()
{
	run_menu();
	return 0;
}

